<?php

class Add_Publish_Article_SavePost
{
    protected $_articleId;
    protected $_data;
    
    public function __construct($articleId, Add_Publish_Article_SavePost_Data $data)
    {
        $this->_articleId = $articleId;
        $this->_data      = $data;
        
        if(!is_array($this->_data->image)){
            $this->_data->image = array();
        }
        
        if(!is_array($this->_data->music)){
            $this->_data->music = array();
        }
        
        if(!is_array($this->_data->video)){
            $this->_data->video = array();
        }
        
        if(!is_array($this->_data->shopping)){
            $this->_data->shopping = array();
        }
        
        if(!is_array($this->_data->recommendTags)){
            $this->_data->recommendTags = array();
        }
    }
    
    public function save()
    {
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article){
            return "";
        }
        
        $row = array(
            'title'    => $this->_data->title,
            'editTime' => time(),
        );                
        App_Model_DbTable_Article::getInstance()->addOld($row, $this->_articleId);
        $row = array(
            'content' => $this->_data->description,
            'htmlOn'  => 1,
        );
        App_Model_DbTable_Article_Desc::getInstance()->add($row, $this->_articleId);
        
        foreach($this->_data->image as $key=>$val){
            $attachmentImageId = $key;
            $title             = $val['title'];
            $description       = $val['description'];
            $descriptionHtmlOn = 0;

            $row = array(
                'title'             => $title,
                'description'       => $description,
                'descriptionHtmlOn' => $descriptionHtmlOn,
            );
            App_Model_DbTable_Attachment_Image::getInstance()->addOld($row, $attachmentImageId);
        }
        
        foreach($this->_data->video as $key=>$val){
            $attachmentVideoId = $key;
            $title             = $val['title'];
            $description       = $val['description'];
            $descriptionHtmlOn = 0;

            $row = array(
                'title'             => $title,
                'description'       => $description,
                'descriptionHtmlOn' => $descriptionHtmlOn,
            );
            App_Model_DbTable_Attachment_Video::getInstance()->addOld($row, $attachmentVideoId);
        }
        
        foreach($this->_data->music as $key=>$val){
            $attachmentMusicId = $key;
            $name              = $val['name'];
            $singer            = $val['singer'];
            $description       = $val['description'];
            $descriptionHtmlOn = 0;

            $row = array(
                'name'              => $name,
                'singer'            => $singer,
                'description'       => $description,
                'descriptionHtmlOn' => $descriptionHtmlOn,
            );
            App_Model_DbTable_Attachment_Music::getInstance()->addOld($row, $attachmentMusicId);
        }
        
        foreach($this->_data->shopping as $key=>$val){
            $attachmentShoppingId = $key;
            $name              = $val['name'];
            $description       = $val['description'];
            $descriptionHtmlOn = 0;

            $row = array(
                'name'              => $name,
                'description'       => $description,
                'descriptionHtmlOn' => $descriptionHtmlOn,
            );
            App_Model_DbTable_Attachment_Shopping::getInstance()->addOld($row, $attachmentShoppingId);
        }
        
        $tags = $this->_data->definedTags;
        for($i=0;$i<count($this->_data->recommendTags);$i++)
        {
            $tags .= ";".trim($this->_data->recommendTags[$i]);
        }
        $tags = Ext_Utility_String::filterTags($tags);
        App_Model_DbTable_Article_Tags::getInstance()->add($tags, $this->_articleId);
        
        App_Model_DbTable_Article_Search_Tags::getInstance()->deleteByArticleId($this->_articleId);
        $words_array = explode(';', $tags);
        for($i=0;$i<count($words_array);$i++)
        {
            $word = $words_array[$i];
            $word = trim($word);
            if(!$word)
            {
                continue;
            }
            elseif(strlen($word)>60)
            {
                continue;
            }
            else
            {
                $inputArray = array(
                    'tagName'        => $word,
                    'createTime'     => time(),
                );            
                $tagId = App_Model_DbTable_Tags::getInstance()->add($inputArray);
                $row = array(
                    'tagId'         => $tagId,
                    'articleId'     => $this->_articleId,
                );
                App_Model_DbTable_Article_Search_Tags::getInstance()->add($row);
            }
        }
        
        $articleSearchThreads = App_Model_DbTable_Article_Search_Threads::getInstance()->getRowByArticleId($this->_articleId);
        if(!$articleSearchThreads){
            $row = array(
                'articleId' => $this->_articleId,
                'userId'    => $article['userId'],
            );
            App_Model_DbTable_Article_Search_Threads::getInstance()->addNew($row);
        }
        
        $row = array(
            'lastArticleId'             => '',
        );
        App_Model_DbTable_User_Profile::getInstance()->addOld($row, $article['userId']);
    }
}
